﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Tb.App.API.Applications.Command.Basic;
using Tb.App.API.Dtos.Rabbit;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.ErrorCode;

namespace Tb.App.API.Controllers.Rabbit
{
    /// <summary>
    /// 基本信息-商品-控制器API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class BasicController : ControllerBase
    {
        private readonly ILogger<BasicController> logger;
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public BasicController(ILogger<BasicController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 基本信息查询
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<List<BasicDTO>>>> GetBasicQuery([FromQuery]GetBasicQueryCommand request)
        {
            try
            {
                logger.LogInformation("-基本信息查询操作日志-");
                return mediator.Send(request);

            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 商品信息查询(绑定)
        /// </summary>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public Task<ApiResult<List<StoreModel>>> GetStore()
        {
            try
            {
                logger.LogInformation("-商品信息查询操作日志-");
                return mediator.Send(new GetStoreCommand());
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 添加溯源商品基本信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns>返回添加任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddBasic(AddBasicCommand request)
        {
            try
            {
                logger.LogInformation("-添加溯源商品基本信息操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改溯源商品基本信息
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回修改任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdBasic(UpdBasicCommand request)
        {
            try
            {
                var UserNickname = User.FindFirstValue("UserNickname");
                request.Updator = UserNickname;
                logger.LogInformation("-修改溯源商品基本信息操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 删除单条溯源基本信息
        /// </summary>
        /// <param name="Id">编号</param>
        /// <returns>返回单条任务</returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DelBasic(int Id)
        {
            try
            {
                logger.LogInformation("-删除单条溯源基本信息操作日志-");
                DelBasicCommand request = new DelBasicCommand
                {
                    Id = Id
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 批量删除溯源基本信息
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回批量删除任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> Handle(AllDelBasicCommand request)
        {
            try
            {
                logger.LogInformation("-批量删除溯源基本信息操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 溯源商品基本信息反填查询
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询任务</returns>
        [HttpGet]
        public Task<ApiResult<BasicDTO>> GetFanBasicQuery([FromQuery]GetFanBasicQueryCommand request)
        {
            try
            {
                logger.LogInformation("-溯源商品基本信息反填查询操作日志-");
                return mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);
                throw;
            }
        }
    }
}
